package Generic;

import java.util.ArrayList;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class GenericImprove {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        //泛型的好处
        //1.编译时，检查添加元素的类型，提高了安全性
        //2.减少了类型转换的次数，提供效率

        ArrayList<Dog1> arrayList = new ArrayList<Dog1>();
        arrayList.add(new Dog1("小黄",1));
        arrayList.add(new Dog1("小黑",2));
        arrayList.add(new Dog1("小白",3));

        //加入不小心添加了一只猫,会提示错误
        //arrayList.add(new Cat1("泡芙",3));

        //遍历
        for (Dog1 dog :arrayList) {
            System.out.println(dog.getName() + "_" + dog.getAge() + "岁");
        }

    }
}
class Dog1{
    private String name;
    private int age;

    public Dog1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat1{
    private String name;
    private int age;

    public Cat1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
